例 ❶ (篩選會回傳篩選合適的第一個值)
const arr = [1, 5, 50];
const newArr = arr.find(function (item) {
return item >= 5;
})
例 ❶
? ? ?
const arr = ['blue', 'red', 'yello'];
const newArr = arr.findIndex(function (item) {
return item == 'blue';
})
例 ❷
const data = [
{
name: "小花",
orders: '132242342'
}, {
name: "小㓬",
orders: '165265222'
}
]
const newOd = data.findIndex(function (item) {
return item.orders == '132242342';
})
console.log(`這筆訂單是${data[newOd].name}的`);
const people = [
{
name: 'denny',
order: '鍋燒意麵',
price: 80
},
{
name: '小明',
order: '牛肉麵',
price: 120
},
{
name: '漂亮阿姨',
order: '滷味切盤',
price: 40
},
{
name: 'Ray',
order: '大麻醬乾麵',
price: 60
}
]
people.forEach(function(obj, key) {
// console.log(obj, key);
});
const newOrders = [];
people.forEach(function(obj, key) {
newOrders[key] = {
...obj,
newPrice: obj.price * 0.8;
}
});
console.log(newOrders);
const newOrders2 = [];
people.forEach(function(item, index) {
if (item.price >= 80) {
newOrders2.push(item)
}
});